/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.martinkrasa.golftourweb.site;

import com.martinkrasa.golftourweb.site.entity.Tournament;
import java.util.LinkedHashMap;
import java.util.Map;
import org.springframework.stereotype.Repository;

@Repository
public class InMemoryTournamentRepository implements TournamentRepository {
    
    private volatile long tournamentIdSequence = 1L;
    private final Map<Long, Tournament> tournamentList = new LinkedHashMap<>();

/*    public InMemoryTournamentRepository() {
        long id = this.getNextTournamentId();
        
        Tournament tournament = new Tournament();
        tournament.setId(id);
        tournament.setSeasonId(0);
        tournament.setWebsiteId("");
        tournament.setName("Test tournament");
        tournament.setType(new TournamentType());
        TournamentResult result = new TournamentResult();
        result.setId(1L);
        result.setTournamentId(id);
        result.setRank(1);
        result.setPlayerName("Test Player");
        result.setClubAbbreviation("TGCC");
        result.setCardId("000/0000");
        result.setNettoResult(36);
        result.setBruttoResult(18);
        result.setResult(90);
        tournament.getResults().add(result);
        
        this.tournamentList.put(id,tournament);
    }*/

    @Override
    public Tournament getTournament(long id) {
        return new Tournament(this.tournamentList.get(id));
    }
 
    @Override
    public void addTournament(Tournament tournamentToAdd) {
        
        long id = this.getNextTournamentId();
        tournamentToAdd.setId(id);

        this.tournamentList.put(id, tournamentToAdd);
    }
    
    private synchronized long getNextTournamentId() {
        return this.tournamentIdSequence++;
    }

}
